//
//  HomeViewController.m
//  7.13苏宁框架
//
//  Created by mini on 15/7/15.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "HomeViewController.h"
#import "Common.h"
#import "Number2Cell.h"
#import "Number3Cell.h"
#import "Number4Cell.h"
#import "Number5Cell.h"
#import "Number6Cell.h"
#import "Number7Cell.h"
#import "Number8Cell.h"
#import "Number9Cell.h"
#import "Number12Cell.h"
#import "NearbyController.h"
#import "MoneyController.h"
#import "MoreViewController.h"
#import "BigFavorableViewController.h"
#import "XQViewController.h"
#import "QRCodeReaderDelegate.h"
#import "DSZMainViewController.h"
#import "MJChiBaoZiHeader.h"
#import "MJRefresh.h"
#import "MBProgressHUD+MJ.h"
#import "QRCodeReaderDelegate.h"
#import "QRCodeReaderViewController.h"
#import "DSZLYViewController.h"
#import "YunZuanController.h"
#import "DSZClassifyViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

-(void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    //显示分栏
    DSZMainViewController *tab = (DSZMainViewController*)self.tabBarController;
    [tab hiddenMainTabbar:NO];
    self.tableView.header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
}

#pragma mark 下拉刷新数据
- (void)loadNewData
{
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        [MBProgressHUD showSuccess:@"刷新成功"];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //添加表，并设置标头
    CGFloat W = self.view.frame.size.width;
    CGFloat H = self.view.frame.size.height;
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, W, H) style: UITableViewStyleGrouped ];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor colorWithRed:248.0/255 green:248.0/255 blue:248.0/255 alpha:1];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _headView = [[HeadView alloc]init];
    _headView.delegate =self;
    self.tableView.tableHeaderView = _headView;
     //添加回顶部按钮
    _returnTopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _returnTopButton.frame = CGRectMake(W-45, self.tableView.bottom-60-45-30,30, 30);
    [_returnTopButton setBackgroundImage:[UIImage imageNamed:@"home_gototop"] forState:UIControlStateNormal];
    //_returnTopButton.backgroundColor = [UIColor redColor];
    [_returnTopButton addTarget:self action:@selector(returnTop) forControlEvents:UIControlEventTouchUpInside];
    _returnTopButton.hidden = YES;
    [self.view addSubview:_returnTopButton];
    //导航栏
    _na= [[FirstNavigationView alloc]initWithFrame:CGRectMake(0, 0, W, 64)];
    _na.hidden = YES;
    [self.view addSubview:_na];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 12;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *cells = [[UITableViewCell alloc]init];
    if (indexPath.row==0) {
        Number1Cell *cell = [[Number1Cell alloc]init];
        cell.delegate = self;
        cells = cell;
    }
    if (indexPath.row==1) {
        Number2Cell *cell = [[Number2Cell alloc]init];
        cells = cell;
    }
    if (indexPath.row==2) {
        Number3Cell *cell = [[Number3Cell alloc]init];
        cells = cell;
    }
    if (indexPath.row==3) {
        Number4Cell *cell = [[Number4Cell alloc]init];
        cells = cell;
    }
    if (indexPath.row==4) {
        Number5Cell *cell = [[Number5Cell alloc]init];
        cells = cell;
    }
    if (indexPath.row==5) {
        Number6Cell *cell = [[Number6Cell alloc]init];
        cells = cell;
    }if (indexPath.row==6) {
        Number7Cell *cell = [[Number7Cell alloc]init];
        cells = cell;
    }if (indexPath.row==7) {
        Number8Cell *cell = [[Number8Cell alloc]init];
        cells = cell;
    }if (indexPath.row==8) {
        Number9Cell *cell = [[Number9Cell alloc]init];
        cells = cell;
    }if (indexPath.row==9) {
        Number10Cell *cell = [[Number10Cell alloc]init];
        cells = cell;
    }if (indexPath.row==10) {
        Number11Cell *cell = [[Number11Cell alloc]init];
        cell.delegate = self;
        cells = cell;
    }if (indexPath.row==11) {
        Number12Cell *cell = [[Number12Cell alloc]init];
        cells = cell;
    }
    cells.selectionStyle = UITableViewCellSelectionStyleNone;
    return cells;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int height = 0;
    if (indexPath.row==0) {
        height = 150;
    }
    if (indexPath.row==1) {
       height = 155;
    }
    if (indexPath.row==2) {
       height = 95;
    }
    if (indexPath.row==3) {
        height = 385;
    }
    if (indexPath.row==4) {
       height = 80;
    }
    if (indexPath.row==5) {
       height = 445;
    }if (indexPath.row==6) {
      height = 90;
    }if (indexPath.row==7) {
      height = 130;
    }if (indexPath.row==8) {
    height = 175;
    }if (indexPath.row==9) {
        height = 35;
    }
    if (indexPath.row==10) {
        height = 1910;
    }
    if (indexPath.row==11) {
        height = 35;
    }
    return height;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        CGFloat W = self.view.frame.size.width;
        CGFloat H = self.view.frame.size.height;
        //隐藏或者显示回到顶部按钮
        if (self.tableView.contentSize.height - self.tableView.contentOffset.y <=2400) {
            _returnTopButton.hidden = NO;
        }
        else
        {
            _returnTopButton.hidden = YES;
        }
        //更换中间视图内容
        if (scrollView.contentOffset.y>1630) {
            NSIndexPath *path = [NSIndexPath indexPathForItem:9 inSection:0];
            _cell10 = (Number10Cell*)[self.tableView cellForRowAtIndexPath:path];
            _cell10.smallImage.image = [UIImage imageNamed:@"home_recommend_up"];
            _cell10.labelText.text = @"下拉返回";
        }
        else
        {
            NSIndexPath *path = [NSIndexPath indexPathForItem:9 inSection:0];
            _cell10 = (Number10Cell*)[self.tableView cellForRowAtIndexPath:path];
            _cell10.smallImage.image = [UIImage imageNamed:@"home_recommend_down"];
            _cell10.labelText.text = @"下面更多惊喜呦";
        }
        //隐藏或者显示导航栏
        if(scrollView.contentOffset.y>=63)
        {
            _na.hidden = NO;
            
        }
    if (self.tableView.contentOffset.y<=63) {
        _na.hidden = YES;
    }
}
//回到顶部的方法
-(void)returnTop
{
    [self.tableView scrollRectToVisible:self.tableView.frame animated:YES];
}

//代理
-(void)nearbyPush
{
    NearbyController *con = [[NearbyController alloc]init];
    [self.navigationController pushViewController:con animated:YES];
}
-(void)moneyPush
{
    MoneyController *con = [[MoneyController alloc]init];
    [self.navigationController pushViewController:con animated:YES];
}
-(void)morePush
{
    MoreViewController *con = [[MoreViewController alloc]init];
    [self.navigationController pushViewController:con animated:YES];
}
-(void)yunzuanPush
{
    YunZuanController *yunzuan = [[YunZuanController alloc]init];
    [self.navigationController pushViewController:yunzuan animated:YES];
    DSZMainViewController *con = [[DSZMainViewController alloc]init];
    [con hiddenMainTabbar:YES];
}

-(void)pushFenlei
{
//    DSZClassifyViewController * con = [[DSZClassifyViewController alloc]init];
//    [self.navigationController pushViewController:con animated:YES];
    
//    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    DSZClassifyViewController *con = [story instantiateViewControllerWithIdentifier:@"cato"];
//    [self.navigationController pushViewController:con animated:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pushFen" object:nil];
}
#pragma 二维码扫描
-(void)saoma
{
    static QRCodeReaderViewController *reader = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        reader = [QRCodeReaderViewController new];
        reader.modalPresentationStyle = UIModalPresentationFormSheet;
    });
    reader.delegate = self;
    
    [reader setCompletionWithBlock:^(NSString *resultAsString) {
    }];
    
    [self presentViewController:reader animated:YES completion:NULL];
}

- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:result]];
        
    }];
}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma 录音
-(void)luyin
{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"DSZLYViewController" bundle:nil];
    DSZLYViewController *con = [story instantiateViewControllerWithIdentifier:@"lv"];
    [self presentViewController:con animated:YES completion:nil];

}
-(void)clickCell0:(NSInteger)index
{
    if (index == 0) {
        BigFavorableViewController *big = [[BigFavorableViewController alloc]init];
        [self.navigationController pushViewController:big animated:YES];
    }
}
//代理跳转到商品详情控制器
-(void)pushQX:(XQModel *)model
{
    _model = model;
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"XQ" bundle:nil];
    XQViewController *con = (XQViewController*)[story instantiateViewControllerWithIdentifier:@"xq"];
    con.model = _model;
    [self.navigationController pushViewController:con animated:YES];
    
}




@end
